<template>
  <el-form ref="form" :model="form" label-width="120px">
    {{parentId}}
    <el-form-item label="添加文件：">
      <el-upload
        class="avatar-uploader"
        action="#"
        :show-file-list="false"
        :on-change="getFile"
        :auto-upload="false"
      >
        <img v-if="imageUrl" :src="imageUrl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">确认</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import {addInfo, addInfoFile} from "../../../../api/resource/info";

export default {
  name: "AddFileEdit",
  props: ["parentId"],
  data(){
    return{
      form:{},
      imageUrl: "",
      file: {}
    }
  },
  methods: {
    onSubmit() {
      const fd = new FormData();
      fd.append("file",this.file)
      addInfo(fd).then(response=>{
        response.parentId = this.parentId;
        console.log(response);
        addInfoFile(response).then(res=>{
          this.$emit("close-dialog2",false)
        });
      })

    },
    closeDialog() {
      this.$emit("close-dialog2",false)
    },
    getFile(file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.file = file.raw;
      console.log(file)
    }
  },
}
</script>

<style scoped>

</style>
